<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class logbook extends CI_Controller 
{
    function __construct(){
            parent::__construct();
            
            $this->load->helper('url');
            $this->load->model('entity_model');
            $this->load->library('session');
            $this->load->helper('date');
  
            //cek session dan tipe member untuk sekuritas web
            if (!$this->session->userdata('username')|| $this->session->userdata('tipemember') !=1 )
            {
                redirect(base_url());
                return;
            } 
    }

//    ===============================================
//    FUNGSI PEMANGGILAN VIEW WEB
//    ===============================================
    public function index()
    {
        $data['username'] = $this->session->userdata('username');
        $data['title'] = 'Logbook Management';
        $data['subtitle'] = '- Home';
        $this->load->view('_header',$data);
        $this->load->view('logbook/logbook_view',$data);
        $this->load->view('_footer',$data);
    }
    
    public function activity()
    {
        //DATETIME
         $datestring = "%D, %d-%M-%Y (%h:%i:%s %A)";
         $datestringSQL = "%Y-%m-%d(%h:%i:%s)";
        
         $totaldate= mdate($datestring, gmt_to_local(time(),'UM7',FALSE));
         $sql = mdate($datestringSQL,gmt_to_local(time(),'UM7',FALSE));
//         echo $totaldate."<br/>".$sql;
        
         $data['username'] = $this->session->userdata('username');
         $data['title'] = 'Logbook Management';
         $data['subtitle'] = '- Manage Activity';
         $data['time'] = $totaldate;
         $data['sqltime'] = $sql;
         $this->load->view('_header',$data);
         $this->load->view('logbook/activity',$data);
         $this->load->view('_footer',$data);
    }
    
//    ===============================================
//    FUNGSI CONTROL DATA
//    ===============================================
    public function submitact()
    {
        $time = $this->input->post('sql_time');
        $nama = $this->input->post('officer');
        $guest = $this->input->post('guest_box');
        $aff = $this->input->post('aff_box');
        $purpose = $this->input->post('purpose_box');
        $counter = $this->input->post('counter_box');
        $status = $time.$nama.$guest.$aff.$purpose.$counter;
        if ($nama&&$time &&$guest && $aff && $purpose && $counter)
        {
            $status = $this->entity_model->submitActivity($time, $nama, 
                    $guest, $aff, $purpose, $counter);
        }
        else
        {
            $status = "Please complete this form!";
        }
        echo json_encode(array('status_submit' => $status));
        exit;
    }
    
    public function showAllActivity()
    {
        $namasearch =  $this->input->post('box_nama');
        $result= $this->entity_model->getAllActivity();
        $html_result = 
        '<table id="table-5">
            <thead>
                <th style="width: 5%">Code</th>
                <th style="width: 10%">Start Time</th>
                <th style="width: 10%">End Time</th>
                <th style="width: 10%">IO Officer</th>
                <th style="width: 10%">Guest Name</th>
                <th style="width: 10%">Affilation</th>
                <th style="width: 15%">Purpose of visit</th>
                <th style="width: 35%">Counter Measure</th>
            </thead>
            <tbody>';
        
        foreach ($result->result() as $row)
        {
            $html_result .='<tr>';
            $html_result .='<td>'.'L'.$row->ID.'</td>';
            $html_result .='<td>'.$row->START_TIME.'</td>';
            $html_result .='<td>'.$row->END_TIME.'</td>';
            $html_result .='<td>'.$row->ACC_NAME.'</td>';
            $html_result .='<td>'.$row->GUEST_NAME.'</td>';
            $html_result .='<td>'.$row->AFFILIATION.'</td>';
            $html_result .='<td>'.$row->PURPOSE.'</td>';
            $html_result .='<td>'.$row->MEASURE.'</td>';
            $html_result .='</tr>';
        }
        $html_result .= 
        '
            </tbody>
        </table>'
        ;
        
        //echo $html_result;
        echo json_encode(array('renderhtml'=>$html_result));
        exit;
    }
    
    
        
}
?>
